package eraserhead.boundary;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSpinner;
import javax.swing.JTextArea;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

import eraserhead.controller.CreateEventController;
import eraserhead.entity.ClosedDecision;
import eraserhead.entity.OpenDecision;
import eraserhead.entity.User;

/**
 * GUI for a moderator to create an event.
 * @author Quanquan
 */
public class CreateEventForm extends JFrame {
	private static final long serialVersionUID = 2014255637142329265L;
	
	User moderator;

	private JButton okBtn;
    private JButton cancelBtn;
    private JLabel questionLabel;
    private JLabel usersLabel;
    private JLabel roundsLabel;
    private JRadioButton openRadioBtn;
    private JRadioButton closedRadioBtn;
    private JScrollPane questionPanel;
    private JSpinner userSpinner;
    private JSpinner roundsSpinner;
    private JTextArea questionField;
    
    SpinnerModel usersModel = new SpinnerNumberModel(3, 3, 8, 1);
    SpinnerModel roundsModel = new SpinnerNumberModel(3, 3, 8, 1);;

    /**
     * Create a new {@link CreateEventForm}
     * @param moderator {@link User} that is destined to be the moderator
     */
	public CreateEventForm(User moderator) {
		this.moderator = moderator;
		initComponents();
	}
	
    private void initComponents() {
        
        questionPanel = new javax.swing.JScrollPane();
        questionField = new javax.swing.JTextArea();
        questionLabel = new javax.swing.JLabel();
        usersLabel = new javax.swing.JLabel();
        roundsLabel = new javax.swing.JLabel();
        userSpinner = new javax.swing.JSpinner(usersModel);
        roundsSpinner = new javax.swing.JSpinner(roundsModel);
        okBtn = new javax.swing.JButton();
        cancelBtn = new javax.swing.JButton();
        openRadioBtn = new javax.swing.JRadioButton();
        closedRadioBtn = new javax.swing.JRadioButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setMaximumSize(new java.awt.Dimension(600, 480));
        setMinimumSize(new java.awt.Dimension(600, 480));
        setPreferredSize(new java.awt.Dimension(600, 480));

        questionField.setColumns(20);
        questionField.setFont(new java.awt.Font("Cambria", 0, 14)); // NOI18N
        questionField.setRows(5);
        questionPanel.setViewportView(questionField);

        questionLabel.setFont(new java.awt.Font("Felix Titling", 0, 18)); // NOI18N
        questionLabel.setText("Question:");

        usersLabel.setFont(new java.awt.Font("Felix Titling", 0, 12)); // NOI18N
        usersLabel.setText("Number of users");

        roundsLabel.setFont(new java.awt.Font("Felix Titling", 0, 12)); // NOI18N
        roundsLabel.setText("Number of ROUNDS");

        userSpinner.setToolTipText("");

        okBtn.setFont(new java.awt.Font("Felix Titling", 0, 11)); // NOI18N
        okBtn.setText("OK");
        okBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                new CreateEventController(CreateEventForm.this).process();
            }
        });

        cancelBtn.setFont(new java.awt.Font("Felix Titling", 0, 11)); // NOI18N
        cancelBtn.setText("Cancel");
        cancelBtn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CreateEventForm.this.dispose();
                WelcomeForm welcomeForm = new WelcomeForm();
                welcomeForm.setVisible(true);
            }
        });

        openRadioBtn.setFont(new java.awt.Font("Felix Titling", 0, 12)); // NOI18N
        openRadioBtn.setText("Open Decision");
        
        closedRadioBtn.setFont(new java.awt.Font("Felix Titling", 0, 12)); // NOI18N
        closedRadioBtn.setText("Closed Decision");

        ButtonGroup bg = new ButtonGroup();
        bg.add(openRadioBtn);
        bg.add(closedRadioBtn);
        openRadioBtn.setSelected(true);
        
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(115, 115, 115)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(usersLabel)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                .addComponent(userSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(roundsLabel)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(roundsSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addGap(45, 45, 45)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(closedRadioBtn)
                            .addComponent(openRadioBtn)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(101, 101, 101)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(okBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(cancelBtn, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(questionLabel, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(questionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 402, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(111, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(60, 60, 60)
                .addComponent(questionLabel)
                .addGap(18, 18, 18)
                .addComponent(questionPanel, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(29, 29, 29)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(usersLabel)
                    .addComponent(userSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(openRadioBtn))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(roundsLabel)
                    .addComponent(roundsSpinner, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(closedRadioBtn))
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(cancelBtn)
                    .addComponent(okBtn))
                .addContainerGap(77, Short.MAX_VALUE))
        );

        pack();
    }
	
	/**
	 * Return the current value in the users spinner
	 * @return User spinner value
	 */
	public int getNumUsers() {
		return (Integer) usersModel.getValue();
	}
	
	/**
	 * Return the current value in the rounds spinner
	 * @return Round spinner value
	 */
	public int getNumRounds() {
		return (Integer) roundsModel.getValue();
	}
	
	/**
	 * Check if the user want to create a open event or closed event
	 * @return True if {@link OpenDecision}, false for {@link ClosedDecision}
	 */
	public boolean getIsOpen() {
		return this.openRadioBtn.isSelected();
	}
	
	/**
	 * Get the text in the question text field
	 * @return Question text
	 */
	public String getQuestion() {
		return this.questionField.getText();
	}
	
	/**
	 * Gets the {@link User} that is the moderator
	 * @return Moderator {@link User} object
	 */
    public User getModerator() {
		return moderator;
	}
	
    /**
     * Sets the question text
     * @param text Question text to set
     */
	public void setQuestion(String text) {
		this.questionField.setText(text);
	}
	
	/**
	 * Get the "open" radio button.
	 * @return The open radio button.
	 */
	public JRadioButton getOpen()
	{
		return openRadioBtn;
	}
	
	/**
	 * Get the "closed" radio button.
	 * @return The closed radio button.
	 */
	public JRadioButton getClosed()
	{
		return closedRadioBtn;
	}
	
	/**
	 * Get the user spinner model
	 * @return User spinner model
	 */
	public SpinnerModel getUsersModel() {
		return usersModel;
	}
}
